package com.kzj.service.impl;

import com.kzj.common.constant.GlobalConstant;
import com.kzj.common.exception.MyException;
import com.kzj.common.result.Result;
import com.kzj.pojo.Course;
import com.kzj.pojo.CourseStudent;
import com.kzj.pojo.Score;
import com.kzj.pojo.Student;
import com.kzj.pojo.dto.ScoreDto;
import com.kzj.pojo.vo.ScoreVo;
import com.kzj.service.ScoreService;
import com.kzj.service.jpaRepository.CourseJpaRepository;
import com.kzj.service.jpaRepository.CourseStudentJpaRepository;
import com.kzj.service.jpaRepository.ScoreJpaRepository;
import com.kzj.service.jpaRepository.StudentJpaRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ScoreServiceImpl implements ScoreService {
    @Autowired
    ScoreJpaRepository scoreJpaRepository;
    @Autowired
    CourseJpaRepository courseJpaRepository;
    @Autowired
    StudentJpaRepository studentJpaRepository;
    @Autowired
    CourseStudentJpaRepository courseStudentJpaRepository;
    @Override
    public Result addScore(Score score) {
        return Result.success(scoreJpaRepository.save(score));
    }

    @Override
    public Result updateScore(Score score) {
        if(score.getValue()>100||score.getValue()<0)
            throw new MyException("分数输入不合法，应在0-100");
        return Result.success(scoreJpaRepository.save(score));
    }

    @Override
    public Result getScoreByTeacherId(Long teacherId) {
        List<Course> courses = courseJpaRepository.getCoursesByTeacherId(teacherId);
        List<Long> courseIds = courses.stream().map(Course::getId).collect(Collectors.toList());
        List<CourseStudent> css = courseStudentJpaRepository.getCourseStudentsByCourseIdIn(courseIds);
        css.stream().map(CourseStudent::getId).collect(Collectors.toList());
        return Result.success(getScoreVo(scoreJpaRepository.getScoresByCourseStudentIdIn(courseIds)));
    }

    @Override
    public Result getScoreVosByCourseIds(List<Long> courseIds) {
        List<CourseStudent> css = courseStudentJpaRepository.getCourseStudentsByCourseIdIn(courseIds);
        List<Long> csIds = css.stream().map(CourseStudent::getId).collect(Collectors.toList());
        return Result.success(getScoreVo(scoreJpaRepository.getScoresByCourseStudentIdIn(csIds)));
    }

    @Override
    public Result deleteById(Long scoreId) {
        scoreJpaRepository.deleteById(scoreId);
        return Result.success();
    }

    @Override
    public Result addScoreByDto(ScoreDto dto) {
        if(dto.getValue()>100||dto.getValue()<0)
            throw new MyException("分数输入不合法，应在0-100");
        if(dto.getCourseId()!=null&&dto.getStudentId()!=null)
        {
            CourseStudent cs = courseStudentJpaRepository.getCourseStudentByCourseIdAndStudentId(dto.getCourseId(), dto.getStudentId());
            if(cs==null)
                throw new RuntimeException(GlobalConstant.PARAMS_EXCEPTION);
            dto.setCourseStudentId(cs.getId());
        }
        if(dto.getCourseStudentId()==null)
            throw new RuntimeException(GlobalConstant.PARAMS_EXCEPTION);
        Score score=new Score();
        BeanUtils.copyProperties(dto,score);
        scoreJpaRepository.save(score);
        return Result.success();
    }

    @Override
    public Result getOptionalTermsByCourseId(Long courseId) {
        List<CourseStudent> css = courseStudentJpaRepository.getCourseStudentByCourseId(courseId);
        List<Integer> terms = css.stream().map(CourseStudent::getTerm).distinct().collect(Collectors.toList());
        return Result.success(terms);
    }



    private ScoreVo getScoreVo(Score score)
    {
        CourseStudent cs = courseStudentJpaRepository.getById(score.getCourseStudentId());
        Student student = studentJpaRepository.getById(cs.getStudentId());
        Course course = courseJpaRepository.getById(cs.getCourseId());
        ScoreVo scoreVo = new ScoreVo();
        BeanUtils.copyProperties(score,scoreVo);
        scoreVo.setStudentId(student.getId());
        scoreVo.setStudentName(student.getFirstname()+student.getLastname());
        scoreVo.setStudentNo(student.getNo());
        scoreVo.setCourseId(course.getId());
        scoreVo.setCourseCode(course.getCode());
        scoreVo.setCourseName(course.getName());
        scoreVo.setTerm(cs.getTerm());
        return scoreVo;
    }
    private List<ScoreVo> getScoreVo(Iterable<Score> scoreIterable)
    {
        List<ScoreVo> result=new ArrayList<>();
        scoreIterable.forEach(score->result.add(getScoreVo(score)));
        return result;
    }
}
